package com.hnkywl.sts.action.cwgl.txcd;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.hnkywl.sts.dao.cwgl.VoucherType;
import com.hnkywl.sts.dao.zjgl.PjdrDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.interceptor.AuthInterceptor;
import com.hnkywl.sts.service.cwgl.FpdrService;
import com.hnkywl.sts.service.cwgl.TxcdService;
import com.hnkywl.sts.service.cwgl.VoucherService;
import com.hnkywl.sts.service.jsgl.FpService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.zjgl.PjglService;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:57
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/txcd")
public class TxcdAction extends SimpleActionSupport {
    private TxcdService txcdService;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //公司
    private Long gsId;
    //状态
    private Integer txcdZt;
    //操作人
    private String czr;

    private Long txcdId;

    private List<VoucherItemData> voucherItems;

    private VoucherItemData voucherItemData;

    private Date txcdSystemDate;

    private Map<String, Object> voucher;

    private Integer attachment;

    private Integer revokeType;

    private Yg yg;

    private Gson gson = new Gson();

    private YgService ygService;

    private XmlParse xmlParse;

    private VoucherService voucherService;

    private Map<String, String> result = new HashMap<String, String>();

    private Map<String, Object> voucherMaster = new HashMap<String, Object>();

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    private String gsmc;

    private PjglService pjglService;
    //备注
    private String bz;

    private String gsMc;

    private String pjhm;

    private Integer voucherNum;

    private String systemDate;

    private PjdrDao pjdrDao;

    private Long djh;

    private Integer txcdPjlx;

    private String rzyh;



    @Action("txcd")
    public String txcd(){
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //贴现承兑查询分页显示
    @Action("txcdList")
    public void ygList() {
        this.renderJson(this.txcdService.findAllTxcd(gsId,gsMc,txcdZt,voucherNum,czr,djh,txcdPjlx,rzyh,page, rows));
    }
    //贴现承兑进入票据页面
    @Action("txcd_insert")
    public String txcdInsert(){
        yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
        gsmc = pjdrDao.get(txcdId).getNbgs().getJc();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        systemDate =fmt.format(new Date()) ;
        voucherMaster = this.txcdService.getVoucherMaster(txcdId, attachment, null, null, null, yg);
        return SUCCESS;
    }

    @Action("findtxcdfl")
    public void findTxcdFl(){
        renderText(txcdService.findQtpzfl(txcdId));
    }

    //财务收付导入
    @Action("generate_voucher")
    public void generateVoucher() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
        try {
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiUsername())) {
                throw new RuntimeException("您没有财务系统用户名，不具有导入权限");
            }
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiPassword())) {
                throw new RuntimeException("您没有财务系统密码，不具有导入权限");
            }
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
            Boolean begin = voucherService.beginGenerateVoucher(txcdId, VoucherType.PJ);
            if (!begin)
                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
//            voucher = this.cwsfService.getVoucher(cwsfId, uuid, null);
            voucher = this.txcdService.getU8VoucherItems(txcdId, uuid, null,txcdSystemDate);
            //凭证头
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            voucher.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    this.txcdService.getVoucherMaster(txcdId,null, uuid,null,null,yg);
            //将头部可以修改的部分的修改值重新赋值
            voucherMaster.put("createdate",simpleDateFormat.format(txcdSystemDate));
            voucherMaster.put("acctyear",simpleDateFormat2.format(txcdSystemDate));
            voucherMaster.put("acctperiod",simpleDateFormat3.format(txcdSystemDate));
            voucherMaster.put("attachment",attachment);
            voucherMaster.put("memo", VoucherType.PJ.getTag()+txcdId);//备注，在凭证删除时使用
            this.txcdService.pjdrChangeDrrq(txcdId,txcdSystemDate);
            voucher.put("master", voucherMaster);
            String r = voucherService.generateVoucher(voucher);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, txcdId, VoucherType.PJ);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }


    /**
     * 取消
     */
    @Action("revoke_note")
    public void revokeNote() {
        String error = this.txcdService.revokeNote(txcdId);
        if (error != null) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, error);
        } else {
            result.put(XmlParse.RESPONSEFLAG, "1");
            result.put(XmlParse.RESPONSEMESSAGE, "取消成功");
        }
        renderJson(gson.toJson(result));
    }

//    /**
//     * 撤销凭证
//     */
//    @Action("revoke_voucher")
//    public void revokeVoucher() {
//        try {
//            Boolean begin = voucherService.beginRevokeVoucher(txcdId, VoucherType.PJ);
//            if (!begin)
//                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
//            Map<String, Object> msgheader = voucherService.getMsgHeader();
//            Map<String, Object> revokeInfo = txcdService.getRevokeInfo(txcdId);
//            Map<String, Object> info = new HashMap<String, Object>();
//            info.put("msgheader", msgheader);
//            info.put("revokeinfo", revokeInfo);
//            String r = voucherService.revokeVoucher(info);
//            result = xmlParse.getContent(r);
//            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
//                Boolean success = voucherService.revokeVoucherSuccess(txcdId, VoucherType.PJ);
//                if (!success)
//                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.put(XmlParse.RESPONSEFLAG, "0");
//            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
//        }
//        renderJson(gson.toJson(result));
//    }

    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
            voucherService.checkJiuqiUsernamePassword();
            Boolean begin = voucherService.beginRevokeVoucher(txcdId, VoucherType.PJ);
            if (!begin)
                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            Map<String, Object> revokeInfo = this.txcdService.getRevokeInfo(txcdId);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            revokeInfo.put("memo", VoucherType.PJ.getTag()+txcdId);//备注，在凭证删除时使用
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            //结果回写
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                Boolean success = voucherService.revokeVoucherSuccess(txcdId, VoucherType.PJ);
                if (!success)
                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 重置凭证
     */
    @Action("rest_voucher")
    public void restVoucher() {
        renderJson(voucherService.restVoucher(txcdId, VoucherType.PJ));
    }

    //==================get and set ================================


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Integer getTxcdZt() {
        return txcdZt;
    }

    public void setTxcdZt(Integer txcdZt) {
        this.txcdZt = txcdZt;
    }


    public String getCzr() {
        return czr;
    }

    public void setCzr(String czr) {
        this.czr = czr;
    }

    public Long getTxcdId() {
        return txcdId;
    }

    public void setTxcdId(Long txcdId) {
        this.txcdId = txcdId;
    }

    public List<VoucherItemData> getVoucherItems() {
        return voucherItems;
    }

    public void setVoucherItems(List<VoucherItemData> voucherItems) {
        this.voucherItems = voucherItems;
    }



    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public Integer getRevokeType() {
        return revokeType;
    }

    public void setRevokeType(Integer revokeType) {
        this.revokeType = revokeType;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }



    public VoucherItemData getVoucherItemData() {
        return voucherItemData;
    }

    public void setVoucherItemData(VoucherItemData voucherItemData) {
        this.voucherItemData = voucherItemData;
    }

    public Date getTxcdSystemDate() {
        return txcdSystemDate;
    }

    public void setTxcdSystemDate(Date txcdSystemDate) {
        this.txcdSystemDate = txcdSystemDate;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public Gson getGson() {
        return gson;
    }

    public void setGson(Gson gson) {
        this.gson = gson;
    }

    public String getGsMc() {
        return gsMc;
    }

    public void setGsMc(String gsMc) {
        this.gsMc = gsMc;
    }

    @Autowired
    public void setTxcdService(TxcdService txcdService) {
        this.txcdService = txcdService;
    }

    public String getPjhm() {
        return pjhm;
    }

    public void setPjhm(String pjhm) {
        this.pjhm = pjhm;
    }

    public Integer getVoucherNum() {
        return voucherNum;
    }

    public void setVoucherNum(Integer voucherNum) {
        this.voucherNum = voucherNum;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }

    public Long getDjh() {
        return djh;
    }

    public void setDjh(Long djh) {
        this.djh = djh;
    }

    public Integer getTxcdPjlx() {
        return txcdPjlx;
    }

    public void setTxcdPjlx(Integer txcdPjlx) {
        this.txcdPjlx = txcdPjlx;
    }

    public String getRzyh() {
        return rzyh;
    }

    public void setRzyh(String rzyh) {
        this.rzyh = rzyh;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }
    @Autowired
    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
    @Autowired
    public void setPjglService(PjglService pjglService) {
        this.pjglService = pjglService;
    }
    @Autowired
    public void setPjdrDao(PjdrDao pjdrDao) {
        this.pjdrDao = pjdrDao;
    }
}
